package Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import Server.Game.Controller;
import Server.Sessions.ActiveSessions;
import Server.Sessions.Broadcaster;
import Server.Sessions.ClientSession;
import Server.Sessions.OutboundMessages;


public class Server {
	private static int PORT = 8888;
	
	public static void main(String[] args) throws IOException {

		ActiveSessions activeSessions = new ActiveSessions();
		OutboundMessages outQueue = new OutboundMessages();

		ServerSocket serv = new ServerSocket(PORT);
		System.out.println("Server started.");
		
		new Controller(activeSessions, outQueue);
		
		new Broadcaster(activeSessions, outQueue);
		
		try {
			while (true) { 									
				Socket sock = serv.accept(); 
				try {
					new ClientSession(sock, activeSessions);
				} catch (IOException e) {
					System.out.println("Sokli viga!");
					sock.close();
				}
			} 

		} finally {
			System.out.println("Server closed.");
			serv.close();
		}
	}
}